package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Test6 {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = null;
        while (!stack.empty() || root != null) {
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            tmp = stack.peek();
            root = stack.pop().right;
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            ret.add(tmp.val);
            root = stack.pop();
        }
        return ret;
    }

    public static void main(String[] args) {
        Test6 test6 = new Test6();
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(3);
        a.right = b;
        b.left = c;
        test6.postorderTraversal(a);
        System.out.println(666);
    }
}
